//
//  MPMoviePlayerView2.m
//  kankanVideo
//
//  Created by wangbo on 7/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MPMoviePlayerView2.h"

static MPMoviePlayerView2 *_playerView;
static MPMoviePlayerViewController *_moviePlayViewController2;
@implementation MPMoviePlayerView2
@synthesize _moviePlayerController,_moviePlayViewController,deleagate;

-(id)init{
    if (!self) {
        self=[super init];
    }
    return self;
}
+(MPMoviePlayerView2*)shareInstance{
    if(!_playerView){
        _playerView=[[MPMoviePlayerView2 alloc]init];
    }
    return _playerView;
}
-(MPMoviePlayerViewController*)getPlayer{
    return _moviePlayViewController;
}
//本地视频事件
-(void) playFileVideo
{//此方法只是 3。2以后的方法
    NSString *path=[[NSBundle mainBundle] pathForResource:@"sophie" ofType:@"mov"];
    NSURL *url=[[NSURL alloc] initFileURLWithPath:path];
    MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:url];
    if (tmpMoviePlayViewController)
    {
        _moviePlayViewController=tmpMoviePlayViewController;
        [_playerView presentMoviePlayerViewControllerAnimated:_moviePlayViewController];
        _moviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        [_moviePlayViewController.moviePlayer play];
    }
    [tmpMoviePlayViewController release];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
//远程视频事件
- (void) playURLVideo:(NSString*)_url
{
    //NSString *videoPath =@"http://ioslive.kankanews.com/live/Stream_1.m3u8";
    if (_url == NULL)
        return;
    [self initAndPlay:_url];
}
-(void) initAndPlay:(NSString *)videoURL
{
    if ([videoURL rangeOfString:@"http://"].location!=NSNotFound||[videoURL rangeOfString:@"https://"].location!=NSNotFound) 
    {
        NSURL *URL = [[NSURL alloc] initWithString:videoURL];
        if (URL) {
            if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
            {//3。2以后
                NSLog(@"after_3_2");
                MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:URL];
                if (tmpMoviePlayViewController)
                {
                    _playerView._moviePlayViewController=tmpMoviePlayViewController;
                    _moviePlayViewController.view.frame=self.view.frame;
                    //_moviePlayViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;//全屏播放（全屏播放不可缺） 
                    _moviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
                    //_moviePlayViewController.moviePlayer.movieSourceType=MPMovieSourceTypeStreaming;
                    [_moviePlayViewController.moviePlayer play];
                    [_playerView.view addSubview:_moviePlayViewController.view];
                    //[self presentMoviePlayerViewControllerAnimated:_moviePlayViewController];
                    NSLog(@"playerController_retain:%d",_moviePlayViewController.retainCount);
                }
                [tmpMoviePlayViewController release];
                /*if (!_moviePlayViewController2) {
                    _moviePlayViewController2=[[MPMoviePlayerViewController alloc]init];
                    [_moviePlayViewController2.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
                    _moviePlayViewController2.moviePlayer.view.frame=self.view.bounds;
                    [_playerView.view addSubview:_moviePlayViewController2.view];
                    //[_moviePlayViewController2 release];
                }
                _moviePlayViewController2.moviePlayer.contentURL=URL;
                //_moviePlayViewController2.moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
                [_moviePlayViewController2.moviePlayer play];*/
                
                //[_moviePlayViewController2 release];
                NSLog(@"player1_retain:%d",_moviePlayViewController.retainCount);
            }
            else if([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2)
            {//3。2以前
                MPMoviePlayerController* tmpMoviePlayController=[[MPMoviePlayerController alloc] initWithContentURL:URL];
                if (tmpMoviePlayController) 
                {
                    _playerView._moviePlayerController=tmpMoviePlayController;
                    [_moviePlayerController play];
                }
                [tmpMoviePlayController release];
            }
            //视频播放完成通知
            
            [[NSNotificationCenter defaultCenter] addObserver:_playerView selector:@selector(playbackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
            
        }
        [URL release];
    }
}
//视频播放完成后事件

- (void) playbackDidFinish
{
    [[NSNotificationCenter defaultCenter] removeObserver:_playerView name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    /*if (_moviePlayViewController2)
    {
        //[_moviePlayViewController2.view removeFromSuperview];
        //[self dismissMoviePlayerViewControllerAnimated];
        [_moviePlayViewController2.moviePlayer stop];
        //_moviePlayViewController2.moviePlayer.initialPlaybackTime=-1.0;
        NSLog(@"player2:%d",_moviePlayViewController2.retainCount);
        //[_moviePlayViewController release];
        //_moviePlayViewController2=nil;
        [deleagate playover];
    }*/
    if (_moviePlayViewController)
    {
        [_moviePlayViewController.view removeFromSuperview];
        //[self dismissMoviePlayerViewControllerAnimated];
        [_moviePlayViewController.moviePlayer stop];
        _moviePlayViewController.moviePlayer.initialPlaybackTime=-1.0;
        NSLog(@"player2:%d",_moviePlayViewController.retainCount);
        [_moviePlayViewController release];
        _moviePlayViewController=nil;
        [deleagate playover];
    }
    if (_moviePlayerController) 
    {
        [_playerView._moviePlayerController stop];
        _moviePlayerController.initialPlaybackTime = -1.0;
        [_moviePlayerController release];
        _moviePlayerController = nil;
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    //CGRect viewInsetRect = CGRectInset ([self.view bounds],0,0);
    NSLog(@"didRotate1");
    CGRect viewInsetRect=CGRectInset([_playerView.view bounds], 0, 0);
	[[_moviePlayViewController2.moviePlayer view] setFrame:viewInsetRect];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end
